package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.BmqInfo;
import org.dtrd.modules.evaluation.entity.dto.BmqDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdBmq;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdBmqMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdBmqService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * <p>
 * 服药信念评估BMQ 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdBmqServiceImpl extends ServiceImpl<DtrdEntEvaluationRdBmqMapper, DtrdEntEvaluationRdBmq> implements IDtrdEntEvaluationRdBmqService {

    @Override
    public BmqInfo saveBMQInfo(BmqDTO dto) {
        StringJoiner joiner = new StringJoiner(",");
        int score = 0;
        int tmpScore;
        for (int i = 0; i < dto.getAnswers().length; i++) {
            tmpScore = dto.getAnswers()[i];
            // 设置答案
            joiner.add(tmpScore+"");
            if (1 == i + 1 || 2 == i + 1 || 3 == i + 1 || 4 == i + 1 || 5 == i + 1) {
                score += tmpScore;
            } else if (6 == i + 1 || 7 == i + 1 || 8 == i + 1 || 9 == i + 1 || 10 == i + 1) {
                score += 1 - tmpScore;
            }
        }
        String output = "您的总分为"+score+"分，服药信念";
        if (score < 6) {
            output += "不理想。";
        } else if (score < 10) {
            output += "适中。";
        } else if (score == 10) {
            output += "较好。";
        }
        DtrdEntEvaluationRdBmq bmq = new DtrdEntEvaluationRdBmq();
        bmq.setCreateTime(new Date());
        bmq.setScore(score);
        bmq.setInput(joiner.toString());
        bmq.setOutput(output);
        // 有用户id就保存到数据库
        if (dto.getPatientId() != null) {
            bmq.setPatientId(dto.getPatientId());
            baseMapper.insert(bmq);
        }
        return new BmqInfo().parseFromPo(bmq);
    }

    @Override
    public BmqInfo getBMQInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdBmq> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdBmq::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdBmq::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdBmq::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdBmq bmq = baseMapper.selectOne(lambdaQueryWrapper);
        BmqInfo bmqInfo = new BmqInfo();
        if (bmq != null) {
            bmqInfo = bmqInfo.parseFromPo(bmq);
        }
        return bmqInfo;
    }

    @Override
    public List<BmqInfo> getBmqInfoList(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdBmq> queryWrapper = baseQueryWrapper()
                .eq(DtrdEntEvaluationRdBmq::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdBmq::getCreateTime);
        List<DtrdEntEvaluationRdBmq> bmqPoList = this.list(queryWrapper);
        List<BmqInfo> bmqInfoList = null;
        if (bmqPoList != null && !bmqPoList.isEmpty()){
            bmqInfoList = bmqPoList.stream()
                    .map(po -> new BmqInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        return bmqInfoList;
    }

    private LambdaQueryWrapper<DtrdEntEvaluationRdBmq> baseQueryWrapper(){
        return Wrappers.lambdaQuery(DtrdEntEvaluationRdBmq.class)
                .eq(DtrdEntEvaluationRdBmq::getIsDel, 0);
    }
}
